<?php
/**
 * 照片
 * @author  zhanghui
 * @create_date 2008-02-27
 */

class Photo{
    function __construct(){}
    
    function __destruct(){}
    
    /* 增加照片 
     * @param : $arr_input -  baby_id,folderid,postid,photo_name,content,file_server,file_name,writer_id,
                              writer_name ,ip_address,create_date
     */
    public function addPhoto($conn,$arr_input){
        $str_query = "insert into";
        $str_query .= " baby_photo ";
        $str_query .= " set";
        $str_query .= " baby_id={$arr_input['baby_id']}";
        $str_query .= ",folderid={$arr_input['folderid']}";
        $str_query .= ",photo_name='{$arr_input['photo_name']}'";
        $str_query .= ",content='{$arr_input['content']}'";
        $str_query .= ",file_server='{$arr_input['file_server']}'";
        $str_query .= ",file_name='{$arr_input['file_name']}'";
        $str_query .= ",writer_id={$arr_input['writer_id']}";
        $str_query .= ",writer_name='{$arr_input['writer_name']}'";
        $str_query .= ",ip_address='{$arr_input['ip_address']}'";
        $str_query .= ",create_date='".$conn->getCurrentTime()."'";
        
        $conn->executeQuery($str_query);
        
        $obj_PhotoCnt = new PhotoCnt();
        $obj_PhotoCnt->initPhotoCnt($conn,$arr_input);
    }
    
    /* 照片编辑
     *@param : $arr_input - baby_id,folderid,postid,photo_name,content,file_server,file_name,writer_id,
                            writer_name ,ip_address,create_date
    */
    public function editPhoto($conn,$arr_input){
    	$str_query = "update";
    	$str_query .= " baby_photo ";
    	$str_query .= " set";
        $str_query .= ",photo_name='{$arr_input['photo_name']}'";
        $str_query .= ",content='{$arr_input['content']}'";
        $str_query .= ",writer_id='{$arr_input['writer_id']}'";
        $str_query .= ",writer_name='{$arr_input['writer_name']}'";
        $str_query .= ",ip_address='{$arr_input['ip_address']}'";
        $str_query .= " where";
        $str_query .= " baby_id = {$arr_input['baby_id']}";
        $str_query .= " folderid = {$arr_input['folderid']}";
        $str_query .= " and postid = {$arr_input['postid']}";
        
        $conn->executeQuery($str_query);
    }
    
    /* 删除照片
     * @param : $arr_input - baby_id,folderid,postid,photo_name,content,file_server,file_name,writer_id,
                             writer_name ,ip_address,create_date
    */
    public function deletePhoto($conn,$arr_input){
    	$str_query = "delete from";
    	$str_query .= " baby_photo ";
    	$str_query .= " where";
    	$str_query .= " baby_id={$arr_input['baby_id']}";
    	$str_query .= " and folderid={$arr_input['folderid']}";
    	
    	if(isset($arr_input['postid'])){
    		$str_query .= " and postid={$arr_input['postid']}";
    		$conn->executeQuery($str_query);
    		
    		$obj_PhotoComment = new PhotoComment();
    	    $obj_PhotoComment->deleteComment($conn,$arr_input); 
    	}else{
    		$conn->executeQuery($str_query);
    		
    		$obj_PhotoComment = new PhotoComment();
    	    $obj_PhotoComment->deleteComment($conn,$arr_input);
    	}
 	
    }
    
    /* 获取照片
     * @param : $arr_input -  baby_id,folderid,postid,photo_name,content,file_server,file_name,writer_id,
                              writer_name ,ip_address,create_date
     * @return : $arr_output 
     *           baby_id,             [0]<br>
     *		     folderid,            [1]<br>
     *           postid,              [2]<br>
     *		     photo_name,          [3]<br>
     *           content,             [4]<br>
     *           file_server,         [5]<br>
     *           file_name,           [6]<br>
     *           writer_id,           [7]<br>
     *           writer_name,         [8]<br>
     *           ip_address,          [9]<br>
     *           create_date,         [10]<br>
    */
    public function getPhotoList($conn,$arr_input,$limit = null){
    	$str_query = "select";
    	if(isset($arr_input['cnt'])){
    		$str_query .= " count(1)";
    	}else{
    		$str_query .= " baby_id";
    		$str_query .= " ,folderid";
    		$str_query .= " ,postid";
    		$str_query .= " ,photo_name";
    		$str_query .= " ,content";
    		$str_query .= " ,title";
    		$str_query .= " ,content";
    		$str_query .= " ,file_server";
    		$str_query .= " ,file_name";
    		$str_query .= " ,writer_id";
    		$str_query .= " ,writer_name";
    		$str_query .= " ,ip_address";
    		$str_query .= " ,create_date";
    	}
    	$str_query .=" from";
    	$str_query .=" baby_photo ";
    	$str_query .=" where";
    	$str_query .=" baby_id={$arr_input['baby_id']}";
    	$str_query .=" and folderid={$arr_input['folderid']}";
    	
    	if(isset($arr_input['postid'])){
    		$str_query .=" and postid={$arr_input['postid']}";
    	}
    	if (isset($arr_input['orderby'])) {
    		$str_query .= " order by {$arr_input['orderby']}";
    	}
    	if ($limit != null){
    		$str_query .= " {$limit}";
    	}

    	$arr_output = $conn->selectQuery($str_query);
    	return $arr_output;
    }
}
?>